#include "QQFileDelegate.h"

QQFileDelegate::QQFileDelegate(QObject* parent)
	: QStyledItemDelegate(parent)
{
}

QQFileDelegate::~QQFileDelegate()
{
}

void QQFileDelegate::setItemHeight(qreal height)
{
	mItemHeight = height;
}

qreal QQFileDelegate::getItemHeight() const
{
	return mItemHeight;
}

void QQFileDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

	QPixmap pixmap = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole));
	QPoint pixmapPos = option.rect.topLeft();
	pixmapPos.setX(option.rect.left() + 10);
	pixmapPos.setY(option.rect.top() + (option.rect.height() - pixmap.height()) / 2);
	painter->drawPixmap(pixmapPos, pixmap);

	QPoint filenamePos = pixmapPos;
	filenamePos.setX(pixmapPos.x() + pixmap.width());
	QString filename = index.data(Qt::DisplayRole).toString();
	QRect filenameRect = QRect(filenamePos, QSize(0, 0));
	filenameRect.setWidth(option.rect.width() - pixmap.width() - 20);
	filenameRect.setHeight((option.rect.height() - 14) / 2);
	painter->setFont(QFont("Microsoft YaHei", 13));
	painter->drawText(filenameRect, Qt::AlignLeft | Qt::AlignVCenter, filename);

	QPoint sizePos = filenameRect.bottomLeft();
	QRect sizeRect = QRect(sizePos, filenameRect.size());
	QString size = index.data(Qt::UserRole).toString();
	painter->setFont(QFont("Microsoft YaHei", 10, QFont::Normal));
	painter->drawText(sizeRect, Qt::AlignLeft | Qt::AlignBottom, size);
}

QSize QQFileDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	return QSize(QStyledItemDelegate::sizeHint(option, index).width(), mItemHeight);
}
